import { create } from 'zustand';
import { Agent } from '../components/AgentManager';
import { McpService, McpTool } from '../components/ToolManager';
import { ChatSession } from '../components/Sidebar';

// 聊天消息类型
interface ChatMessage {
  role: 'user' | 'assistant';
  content: string;
}

/**
 * @deprecated 请使用分拆的 store:
 * - useChatStore (聊天相关状态)
 * - useAgentStore (Agent 相关状态)
 * - useMcpStore (MCP 相关状态)
 * - useUIStore (UI 相关状态)
 *
 * 这个 store 已被拆分为多个独立的 store 以提高性能和可维护性。
 */
// 定义状态类型
interface AppState {
  // UI 状态
  inputText: string;
  sidebarOpen: boolean;
  isLoading: boolean;

  // 选中状态
  selectedAgent: Agent | null;

  // 数据状态
  chatMessages: ChatMessage[];
  chatSessions: ChatSession[];
  currentSessionId: string | undefined;
  agents: Agent[];
  mcpServices: McpService[];
  mcpTools: McpTool[];

  // UI 状态 setters
  setInputText: (text: string) => void;
  setSidebarOpen: (open: boolean) => void;
  setLoading: (loading: boolean) => void;
  setSelectedAgent: (agent: Agent | null) => void;

  // 聊天消息 setters
  setChatMessages: (messages: ChatMessage[]) => void;
  addChatMessage: (message: ChatMessage) => void;
  clearChatMessages: () => void;

  // 会话 setters
  setChatSessions: (sessions: ChatSession[]) => void;
  setCurrentSessionId: (id: string | undefined) => void;
  addChatSession: (session: ChatSession) => void;
  removeChatSession: (id: string) => void;
  updateChatSession: (id: string, updates: Partial<ChatSession>) => void;

  // Agent setters
  setAgents: (agents: Agent[]) => void;
  addAgent: (agent: Agent) => void;
  removeAgent: (id: string) => void;

  // MCP setters
  setMcpServices: (services: McpService[]) => void;
  addMcpService: (service: McpService) => void;
  removeMcpService: (id: string) => void;
  setMcpTools: (tools: McpTool[]) => void;
}

/**
 * @deprecated 请使用分拆的 store:
 * - useChatStore (聊天相关状态)
 * - useAgentStore (Agent 相关状态)
 * - useMcpStore (MCP 相关状态)
 * - useUIStore (UI 相关状态)
 *
 * 这个 store 已被拆分为多个独立的 store 以提高性能和可维护性。
 */
// 创建 store
export const useAppStore = create<AppState>()((set) => ({
  // 初始状态
  inputText: '',
  sidebarOpen: false,
  isLoading: false,
  selectedAgent: null,
  chatMessages: [],
  chatSessions: [],
  currentSessionId: undefined,
  agents: [],
  mcpServices: [],
  mcpTools: [],

  // UI 状态 setters
  setInputText: (text) => set({ inputText: text }),
  setSidebarOpen: (open) => set({ sidebarOpen: open }),
  setLoading: (loading) => set({ isLoading: loading }),
  setSelectedAgent: (agent) => set({ selectedAgent: agent }),

  // 聊天消息 setters
  setChatMessages: (messages) => set({ chatMessages: messages }),
  addChatMessage: (message) => set((state) => ({
    chatMessages: [...state.chatMessages, message]
  })),
  clearChatMessages: () => set({ chatMessages: [] }),

  // 会话 setters
  setChatSessions: (sessions) => set({ chatSessions: sessions }),
  setCurrentSessionId: (id) => set({ currentSessionId: id }),
  addChatSession: (session) => set((state) => ({
    chatSessions: [session, ...state.chatSessions]
  })),
  removeChatSession: (id) => set((state) => ({
    chatSessions: state.chatSessions.filter(session => session.id !== id),
    // 如果删除的是当前会话，清除当前会话 ID
    currentSessionId: state.currentSessionId === id ? undefined : state.currentSessionId
  })),
  updateChatSession: (id, updates) => set((state) => ({
    chatSessions: state.chatSessions.map(session =>
      session.id === id ? { ...session, ...updates } : session
    )
  })),

  // Agent setters
  setAgents: (agents) => set({ agents }),
  addAgent: (agent) => set((state) => ({
    agents: [...state.agents, agent]
  })),
  removeAgent: (id) => set((state) => ({
    agents: state.agents.filter(agent => agent.id !== id)
  })),

  // MCP setters
  setMcpServices: (services) => set({ mcpServices: services }),
  addMcpService: (service) => set((state) => ({
    mcpServices: [...state.mcpServices, service]
  })),
  removeMcpService: (id) => set((state) => ({
    mcpServices: state.mcpServices.filter(service => service.id !== id)
  })),
  setMcpTools: (tools) => set({ mcpTools: tools }),
}));
